import 'package:cunyutech_flutter_core/cunyutech_flutter_core.dart';
import 'package:flutter/widgets.dart';

enum TapFactor {
  /// 节流
  throttle, 
  /// 防抖
  debounce,
}

class CYGestureDetector extends StatefulWidget {
  CYGestureDetector({Key key, this.child, this.throttle = 2000, this.debounce = 300, this.factor = TapFactor.throttle, this.onTap, }) : super(key: key);

  final Widget child;
  final int throttle;
  final VoidCallback onTap;
  final TapFactor factor;
  final int debounce;

  @override
  _CYGestureDetectorState createState() => _CYGestureDetectorState();
}

class _CYGestureDetectorState extends State<CYGestureDetector> {

  Function onTap;

  @override
  void initState() {
    if(widget.onTap != null) {
      if(widget.factor == TapFactor.throttle)
        onTap = RequestAction.throttle(()=> widget.onTap(), widget.throttle);
      else 
        onTap = RequestAction.debounce(()=> widget.onTap(), widget.debounce);
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: widget.child,
    );
  }
}